﻿using System.Reflection;
using Microsoft.Extensions.DependencyInjection;

namespace Athena.ProxyGenerator
{
    using Convertible;
    using Implementaions;
    using Runtime.Client;
    public static class ServiceCollectionExtension
    {
        public static IServiceCollection AddAthenaProxy(this IServiceCollection serviceCollection, string[] proxyAssemblys = null, string[] proxyAssemblyPaths = null)
        {
            serviceCollection.AddSingleton<IServiceProxyGenerater, ServiceProxyGenerater>();
            serviceCollection.AddSingleton<IServiceProxyFactory>((serviceProvider) =>
            {
                var factory = new ServiceProxyFactory(serviceProvider.GetRequiredService<IServiceProxyGenerater>(), serviceProvider.GetRequiredService<IRpcInvokeService>(), serviceProvider.GetRequiredService<ITypeConvertibleService>());

                factory.Initialization(proxyAssemblys, proxyAssemblyPaths);
                return factory;
            });

            return serviceCollection;
        }
    }
}
